﻿/// <reference path="jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../const/sw-const.js"/>
/// <reference path="sw-prototype.js"/>
/// <reference path="sw.js"/>
/// <reference path="sw-control-core.js"/>

$icontrol = function (panel) {
    /// <summary>ipsa2.0 控件库,初始化控件</summary>
    /// <param name="panel" type="JQSelect">容器的选择器的字串,如：id："#id",tag:"tag"</param>
    //初始化proInput
    $icontrol.proInput(panel);
}

///#region proInput 2010-7-16 RoyZhang
///1.2010-7-19 Roy：加入json赋值给控件的方法$icontrol.proInput.setValue
$icontrol.proInput = function (panel) {
    /// <summary>
    /// 根据定义的控件类型itype属性,初始化input录入控件.
    /// itype类型:txt(文本)、date（日期）、cur（货币）、num（数字）
    ///</summary>    
    /// <param name="panel" type="JQSelect">容器的选择器的字串,如：id："#id",tag:"tag"</param>
    var inputs = panel ? $(panel + " input[itype]," + panel + " textarea[itype]") : $(":input[itype],textarea[itype]");

    inputs.each(function (i) {
        var ctr = $(this);
        var type = ctr.attr("itype");
        ctr.addClass("txt");
        switch (type) {
            case "num":
                ctr.addClass("num");
                break;
            case "date":
                ctr.addClass("date");
                break;
            case "cur":
                ctr.addClass("cur");
                break;
            case "url":
                ctr.addClass("url");
                break;
            case "email":
                ctr.addClass("email");
                break;
            case "area":
                ctr.addClass("area");
                break;
            case "ddl":
            case "list":
                $icontrol.chooser(ctr,type);
                break;
        }
    });
}
///#endregion

///#region datepicker
/// <reference path="../plugins/datepicker.js"/>
$icontrol.datepicker = function (ctr,options) {
    /// <summary>初始化Menu</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">input控件</param> 
    /// <param name="options" type="Array">【非必要属性】,datepicker的设置项</param>
    $(ctr).datepicker(options);
}
///#endregion

///#region list 2010-7-20 RoyZhang
$icontrol.list = function (container, template, jsndatas) {
    /// <summary>根据模板数据生成列表Repeat</summary>    
    /// <param name="container" type="StringorJQSelect">容器的选择器的字串</param>
    /// <param name="template" type="StringorJQIDSelect">模板字串(如：<li lv="${Key}">${Value}</li>)或者是包括模板的对象选择器的ID,如:"#id"</param>
    /// <param name="jsndatas" type="Json">Json格式数据集合</param>

    return ictrcore.teml(container, template, jsndatas);
}

$icontrol.list.ajax = function (container, template, fncode, postdata, fnsucess, fnerror) {
    /// <summary>根据模板数据生成列表Repeat</summary>    
    /// <param name="container" type="JQuerySelect">容器的选择器的字串</param>
    /// <param name="template" type="DomStrorJQueryIDSelect">模板字串(如：<li lv="${Key}">${Value}</li>)或者是包括模板的对象选择器的ID,如:"#id"</param>
    /// <param name="fncode" type="String">Function Code</param>    
    /// <param name="postdata" type="StringorJson">Post Data json字串或是Json对象</param>
    /// <param name="fnsucess" type="Function">成功后执行的操作,success(data,container),data：json数据、container容器控件</param>
    /// <param name="fnerror" type="function">请求失败后的操作</param>
    ictrcore.temlbyAjax(container, template, fncode, postdata, fnsucess, fnerror);
}
///#endregion

///#region chooser 2010-7-21 RoyZhang
/// <reference path="../plugins/chooser.js"/>
$icontrol.chooserinit = function (ctr, options, jsndata) {
    /// <summary>初始化选择控件，注：控件设置属性ename，可以读取定义的枚举数据源</summary>
    /// <param name="options" type="Array">设置项</param>
    /// <param name="jsnData" type="Json">【非必要属性】,Json格式数据集合（非ajax方式）</param>
    /// <returns type="JQDom" />
    options.css = iconfig.chooser.css;
    if (!jsndata && options.ename && options.ename.length > 0)
        jsndata = $sw.getEnum(options.ename); //当为枚举数据源时

    return $(ctr).chooser(options, jsndata);
}

$icontrol.chooser = function (ctr,  options) {
    /// <summary>初始化选择控件，注：控件设置属性ename，可以读取定义的枚举数据源</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="options" type="Array">Grid的设置项</param>
    /// <returns type="JQDom" />
    ctr = $(ctr);
    if (!options)
        options = {};
    var ename = ctr.attr('ename');
    var jsnData, ajax;
    if (ename && ename.length > 0)
        jsnData = $sw.getEnum(ename);
    else
        options.ajax = ictrcore.setPsaAjax(ctr.attr('fncode'));

    return $icontrol.chooser.bind(ctr, options, jsnData);
}
$icontrol.chooser.bind = function (ctr, options, jsndata) {
    /// <summary>初始化选择控件使用数据源</summary>    
    /// <param name="ctr" type="JQDom">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="options" type="Array">Grid的设置项</param>
    /// <param name="jsnData" type="Json">【非必要属性】,Json格式数据集合（非ajax方式）</param>
    /// <returns type="JQDom" />
    if (!options)
        options = {};
    if (options.type == 'list')
        return ctr.chooser(options, jsndata);
    else
        return ctr.chooser(options, jsndata);
}
$icontrol.chooser.setValue = function (id, value) {
    /// <summary>初始化选择控件使用数据源</summary>    
    /// <param name="ctr" type="JQDom">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="value" type="String">需要设置的值</param>
    /// <returns type="JQDom" />
    return $(id).chooserSetValue(value);
}
$icontrol.chooser.reload = function (ajax, datas) {
    /// <summary>初始化选择控件使用数据源</summary>    
    /// <param name="ajax" type="JQDom">ajax设置</param>
    /// <param name="datas" type="String">需要设置的值</param>
    /// <returns type="JQDom" />
    return $(id).chooserReload(ajax, datas);
}
///#endregion

///#region modalwin
$icontrol.modalwin = function (url, id, title, width, height) {
    ///<summary>打开模态窗口</summary>
    ///<param name="uicode" type="String">目标地址</param>
    ///<param name="id" type="String">弹出win的id</param>
    ///<param name="title" type="String">win的title</param>   
    ///<param name="width" type="Number">窗口宽度</param> 
    ///<param name="height" type="Number">窗口高度</param>
    $.modalWin({ id: id, url: url, title: title, width: width, height: height });
};
$icontrol.modalwinClose = function (id){
///<summary>关闭模态窗口</summary>
    ///<param name="id" type="String">弹出win的id</param>
    $.modalPanelClose(id,'win');
}
///#endregion

///#region message
$icontrol.alert = function (msg, type) {
    ///<summary>弹出消息方法</summary>
    ///<param name="msg" type="String">弹出消息</param>   
    /// <param name="type" type="String">【非必要属性】,消息类型</param>
    $.message(msg,{showtype:type});
}
$icontrol.alertAndGoto = function (msg,url, type) {
    ///<summary>弹出消息方法</summary>
    ///<param name="msg" type="String">弹出消息</param>   
    /// <param name="type" type="String">【非必要属性】,消息类型</param>
    $.message(msg, { showtype: type, fnok: function () { $sw.GotoPage(url); } });
}
$icontrol.show = function (msg, type, timeout) {
    ///<summary>弹出消息方法</summary>
    ///<param name="msg" type="String">显示消息</param>   
    /// <param name="type" type="String">【非必要属性】,消息类型</param>    
    /// <param name="timeout" type="Number">【非必要属性】,显示时间</param>
    $.message(msg, { type: 'show', showtype: type, timeout: timeout });
}
$icontrol.confirm = function (msg, fnpass, fncancel) {
    ///<summary>confirm操作</summary>
    ///<param name="msg"  type="String">确认消息</param>
    ///<param name="fnpass"  type="Function">通过执行的方法</param>   
    ///<param name="fncancel"  type="Function">【非必要属性】,取消执行的方法</param>
    if (!msg)
        msg = $iconst.Common.confirm;
    $.message(msg, { type: 'confirm', fnok: fnpass, fncancel: fncancel });
};
///#endregion

///#region grid 2010-7-29 RoyZhang
/// <reference path="../plugins/gridview.js"/>
$icontrol.grid = function (ctr, options, fncode, postdata) {
    /// <summary>初始化grid控件</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="options" type="Array">Grid的设置项</param>
    /// <param name="fncode" type="String">Function Code</param>    
    /// <param name="postdata" type="StringorJson">【非必要属性】,Post Data json字串或是Json对象</param>
    /// <param name="fnsucess" type="Function">【非必要属性】,成功后执行的操作,fnsucess()</param>
    options.ajax = ictrcore.setPsaAjax(fncode, postdata);
    $(ctr).gridview(options);
}
$icontrol.grid.reload = function (ctr, fncode, postdata, options) {
    /// <summary>grid reload</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="fncode" type="String">Function Code</param>    
    /// <param name="postdata" type="StringorJson">【非必要属性】,Post Data json字串或是Json对象</param>
    if (!options && fncode) {
        options = {};
        options.ajax = ictrcore.setPsaAjax(fncode, postdata);
    }
    $(ctr).girdviewReload(options);
}
///#endregion

///#region pager 2010-8-2 RoyZhang
/// <reference path="../plugins/pager.js"/>
$icontrol.pager = function (ctr, totalrecord, fnpagechanged, options) {
    /// <summary>初始化翻页控件</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="totalrecord" type="String">总记录数</param>    
    /// <param name="fnpagechanged" type="Function">翻页执行的操作</param>
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    options.totalrecord = totalrecord;
    $(ctr).pager(options, fnpagechanged);
}
$icontrol.pager.change = function (ctr, pageindex, fnpagechanged) {
    /// <summary>翻页控件翻页方法</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="pageindex" type="Number">页码</param>
    /// <param name="fnpagechanged" type="Function">【非必要属性】,成功后执行的操作,success()</param>
    $(ctr).pagerChange(pageindex, fnpagechanged);
}
///#endregion

///#region repeat 2010-9-1 RoyZhang
/// <reference path="../plugins/repeat.js"/>
$icontrol.repeat = function (ctr, template, fncode, options, postdata) {
    /// <summary>初始化grid控件</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="options" type="Array">设置项</param>
    /// <param name="fncode" type="String">Function Code</param>    
    /// <param name="postdata" type="StringorJson">【非必要属性】,Post Data json字串或是Json对象</param>
    /// <param name="fnsucess" type="Function">【非必要属性】,成功后执行的操作,fnsucess()</param>
    if (!options)
        options = {};
    options.ajax = ictrcore.setPsaAjax(fncode, postdata);
    $(ctr).repeat(options, template);
}
$icontrol.repeat.reload = function (ctr, fncode, postdata, options) {
    /// <summary>grid reload</summary>    
    /// <param name="ctr" type="DomStrorJQSelect">触发选择的控件,类型可以为对象也可以为JQ选择字符</param>
    /// <param name="fncode" type="String">Function Code</param>    
    /// <param name="postdata" type="StringorJson">【非必要属性】,Post Data json字串或是Json对象</param>
    if (!options && fncode) {
        options = {};
        options.ajax = ictrcore.setPsaAjax(fncode, postdata);
    }
    $(ctr).repeatReload(options);
}
///#endregion

///#region menu
/// <reference path="../plugins/menu.js"/>
$icontrol.menu = function (selectstr, datas, options) {
    /// <summary>初始化Menu</summary>    
    /// <param name="selectstr" type="JQSelect">menu的容器控件</param>
    /// <param name="datas" type="Json">【非必要属性】,数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    $(selectstr).menu(datas, options);
}
$icontrol.menu.ajax = function (selectstr, options, fncode, postdata) {
    /// <summary>Ajax方式生产menu</summary>    
    /// <param name="selectstr" type="JQSelect">menu的容器控件</param>
    /// <param name="datas" type="Json">数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    ictrcore.GetDatas(fncode, postdata, function (datas) {
        if(datas)
            $(selectstr).menu(datas, options);
     });
}
$icontrol.accordion = function (selectstr, datas, options) {
    /// <summary>初始化翻页控件</summary>    
    /// <param name="selectstr" type="Select">accordion的容器控件</param>
    /// <param name="datas" type="Json">数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    $(selectstr).accordion(datas, options);
}
$icontrol.accordion.ajax = function (selectstr, options, fncode, postdata) {
    /// <summary>初始化翻页控件</summary>    
    /// <param name="selectstr" type="Select">accordion的容器控件</param>
    /// <param name="datas" type="Json">数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    ictrcore.GetDatas(fncode, postdata, function (datas) {
        if (datas)
            $(selectstr).accordion(datas, options);
    });
}
///#endregion

///#region tabs
/// <reference path="../plugins/tabs.js"/>
$icontrol.tabs = function (selectstr, ctrcontent, datas, options) {
    /// <summary>初始化Menu</summary>    
    /// <param name="selectstr" type="JQSelect">选择字串</param>
    /// <param name="ctrcontent" type="JQSelect">加载的内容区域</param>
    /// <param name="datas" type="Json">【非必要属性】,数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    $(selectstr).tabs(ctrcontent, datas, options);
}
$icontrol.tabs.ajax = function (selectstr, ctrcontent, datas, options, fncode, postdata) {
    /// <summary>初始化翻页控件</summary>    
    /// <param name="selectstr" type="JQSelect">选择字串</param>
    /// <param name="ctrcontent" type="DomStrorJQSelect">加载的内容区域</param>
    /// <param name="datas" type="Json">数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    ictrcore.GetDatas(fncode, postdata, function (datas) {
        if (datas)
            $(selectstr).tabs(ctrcontent, datas, options);
    });
}
$icontrol.tabs.select = function (ctr, index) {
    /// <summary>根据index选择tab</summary>    
    /// <param name="ctr" type="JQSelect">tab控件选择字串</param>
    /// <param name="index" type="Number">显示的tab的Index</param>
    $.tabSelect(ctr, index);
}
///#endregion

///#region tree
/// <reference path="../plugins/tree.js"/>
$icontrol.tree = function (selectstr, datas, options) {
    /// <summary>初始化Tree</summary>    
    /// <param name="selectstr" type="JQSelect">选择字串</param>
    /// <param name="datas" type="Json">【非必要属性】,数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    $(selectstr).tree(datas, options);
}
$icontrol.tree.ajax = function (selectstr, options, fncode, postdata) {
    /// <summary>Ajax方式生产tree</summary>
    /// <param name="selectstr" type="JQSelect">选择字串</param>
    /// <param name="datas" type="Json">数据源</param>   
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    ictrcore.GetDatas(fncode, postdata, function (datas) {
        if (datas)
            $(selectstr).tree(datas, options);
    });
}
$icontrol.tree.getTreeValue = function (treeid, chkprop, valuesplit) {
    /// <summary>获取tree中选择的值,仅对多选有效</summary>
    /// <param name="treeid" type="String">tree的ID</param>
    /// <param name="chkprop" type="Json">【非必要属性】,chk定义的属性名称,默认'v'</param>   
    /// <param name="valuesplit" type="String">【非必要属性】,值的分割符，默认取系统统一定义</param>
    /// <returns type="String" />
   return $.treeGetValue(treeid, chkprop, valuesplit);
}
///#endregion

///#region tooltip
/// <reference path="../plugins/tooltip.js"/>
$icontrol.tooltip = function (selectstr, options) {
    /// <summary>tooltip</summary>    
    /// <param name="selectstr" type="JQSelect">选择字串</param>
    /// <param name="options" type="Array">【非必要属性】,属性设置项</param>
    $(selectstr).tooltip(options);
}
///#endregion


